package hlhj.fhp.tvlib.adapter

import android.graphics.Color
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.tenma.ventures.bean.utils.TMSharedPUtil
import com.tenma.ventures.share.bean.TMLinkShare
import com.tenma.ventures.share.util.TMShareUtil
import hlhj.fhp.tvlib.Contents
import hlhj.fhp.tvlib.R
import hlhj.fhp.tvlib.customView.GoLoginDialog
import hlhj.fhp.tvlib.javabean.*

/**
 * Created by Never Fear   on 2018\9\6 0006.
Never More....
 */
class LiveNewAdp(private val datas:ArrayList<LiveListBean.DataBean>)
    :BaseQuickAdapter<LiveListBean.DataBean,BaseViewHolder>(R.layout.live_new_item,datas) {
    override fun convert(helper: BaseViewHolder?, item: LiveListBean.DataBean?) {
        Glide.with(mContext).load(Contents.IMG_URL+item?.live_thumb).into(helper!!.getView(R.id.gsyPlayer))
        helper?.setText(R.id.itemTittle,item?.live_title)
        helper?.setText(R.id.zanNum,item?.laud_num.toString())
        helper?.setText(R.id.commentNum,item?.comment_num.toString())
        helper?.setText(R.id.viewNum,item?.read_num.toString())
        /*分享*/
        val btShare = helper?.getView<ImageView>(R.id.btShare)
        btShare.setOnClickListener {
            val instance = TMShareUtil.getInstance(mContext)
            val tmLinkShare = TMLinkShare()
            tmLinkShare.thumb="${Contents.IMG_URL+item?.live_thumb}"
            tmLinkShare.title=item?.live_title
            tmLinkShare.description=item?.live_title
            tmLinkShare.url="${Contents.BaseUrl+"/hlhjlive/Api/download"}"
            instance.shareLink(tmLinkShare)
        }
        //状态
        when (item?.live_status){
            1->{
                if (item?.live_type==1){
                    helper?.setText(R.id.imgType,"正在直播")
                }else{
                    helper?.setText(R.id.imgType,"直播回放")
                }
            }
            2->{
                if (item?.live_type==1){
                    helper?.setText(R.id.imgType,"图文直播")
                }else{

                }
            }
        }
        //收藏
        var extendStr=""
        val collectParamsBean = CollectParamsBean()
        collectParamsBean.ID=item?.id!!
        val extendBean = ExtendBean()
        val androidInfoBean = ExtendBean.AndroidInfoBean()
        val iosInfoBean = ExtendBean.IosInfoBean()
        androidInfoBean.isNativeX=true
        androidInfoBean.paramStr=Gson().toJson(collectParamsBean)
        androidInfoBean.src="hlhj.fhp.tvlib.activitys.NewLiveAty"
        androidInfoBean.wwwFolder=""

        iosInfoBean.isNativeX=true
        iosInfoBean.paramStr=Gson().toJson(collectParamsBean)
        iosInfoBean.src="HLHJNewLiveDetilController"
        iosInfoBean.wwwFolder=""
        extendBean.androidInfo=androidInfoBean
        extendBean.iosInfo=iosInfoBean

        extendStr=Gson().toJson(extendBean)

        val btColl = helper?.getView<ImageView>(R.id.btColl)
        if (item?.is_collection==1){
            btColl.setImageResource(R.drawable.ic_home_collection_selected)
            btColl.setOnClickListener {
                OkGo.post<String>(Contents.BaseUrl+Contents.CancelColl)
                        .headers("token",TMSharedPUtil.getTMToken(mContext))
                        .params("star_id",item?.sid)
                        .execute(object :StringCallback(){
                            override fun onSuccess(response: Response<String>?) {
                                val body = response?.body()
                                val fromJson = Gson().fromJson(body, BaseBean::class.java)
                                if (fromJson.code==200){
                                    Toast.makeText(mContext,"取消成功",Toast.LENGTH_SHORT).show()
                                    OkGo.post<String>(Contents.BaseUrl+Contents.rememberColl)
                                            .params("id",item?.id)
                                            .params("token",TMSharedPUtil.getTMToken(mContext))
                                            .headers("token",TMSharedPUtil.getTMToken(mContext))
                                            .params("type",2)
                                            .params("sid",item?.sid)
                                            .execute(object :StringCallback(){
                                                override fun onSuccess(response: Response<String>?) {

                                                }
                                            })
                                    datas[helper?.adapterPosition].is_collection=0
                                    notifyDataSetChanged()
                                }
                            }
                        })
            }
        }else{
            btColl.setImageResource(R.drawable.ic_sc_normal)
            btColl.setOnClickListener {
                OkGo.post<String>(Contents.BaseUrl+Contents.addColl)
                        .headers("token",TMSharedPUtil.getTMToken(mContext))
                        .params("member_code",TMSharedPUtil.getTMUser(mContext).member_code)
                        .params("title",item?.live_title)
                        .params("intro",item?.live_title)
                        .params("app_id",Contents.AppId)
                        .params("article_id",item?.id)
                        .params("tag","")
                        .params("type",2)
                        .params("pic","${Contents.IMG_URL+item?.live_thumb}")
                        .params("extend",extendStr)
                        .execute(object :StringCallback(){
                            override fun onSuccess(response: Response<String>?) {
                                val body = response?.body()
                                try {
                                    val baseBean = Gson().fromJson(body!!, BaseBean::class.java)
                                    if (baseBean.code==200){
                                        val collBean = Gson().fromJson(body!!, CollBean::class.java)
                                        if(collBean.code==200){
                                            datas[helper?.adapterPosition].is_collection=1
                                            Toast.makeText(mContext,"收藏成功",Toast.LENGTH_SHORT).show()
                                            datas[helper?.adapterPosition].sid=collBean.data.star_id
                                            OkGo.post<String>(Contents.BaseUrl+Contents.rememberColl)
                                                    .params("id",item?.id)
                                                    .params("token",TMSharedPUtil.getTMToken(mContext))
                                                    .headers("token",TMSharedPUtil.getTMToken(mContext))
                                                    .params("type",2)
                                                    .params("sid",item?.sid)
                                                    .execute(object :StringCallback(){
                                                        override fun onSuccess(response: Response<String>?) {

                                                        }
                                                    })
                                            notifyDataSetChanged()
                                        }
                                    }else{
                                        GoLoginDialog(mContext).show()
                                    }

                                }catch (e:Exception){
                                    GoLoginDialog(mContext).show()
                                }

                            }
                        })
            }
        }
        //赞
        val loZan = helper?.getView<LinearLayout>(R.id.loZan)
        if (item?.is_laud==1){
            helper?.setImageResource(R.id.imgZan,R.drawable.ic_home_praise_select)
            helper?.getView<TextView>(R.id.zanNum).setTextColor(Color.parseColor("#C41717"))
            loZan.setOnClickListener {
                OkGo.post<String>(Contents.BaseUrl+Contents.ItsGood)
                        .params("token", TMSharedPUtil.getTMToken(mContext))
                        .headers("token", TMSharedPUtil.getTMToken(mContext))
                        .params("id",item?.id)
                        .params("type",1)
                        .execute(object : StringCallback(){
                            override fun onSuccess(response: Response<String>?) {
                                val body = response?.body()
                                val baseBean = Gson().fromJson(body, BaseBean::class.java)
                                if (baseBean.code==1){
                                    datas[helper?.adapterPosition].laud_num--
                                    datas[helper?.adapterPosition].is_laud=0
                                    notifyDataSetChanged()
                                }else{
                                    GoLoginDialog(mContext).show()
                                }
                            }
                        })
            }
        }else{
            helper?.setImageResource(R.id.imgZan,R.drawable.ic_home_praise_normal)
            helper?.getView<TextView>(R.id.zanNum).setTextColor(Color.parseColor("#999999"))
            loZan.setOnClickListener {
                OkGo.post<String>(Contents.BaseUrl+Contents.ItsGood)
                        .params("token", TMSharedPUtil.getTMToken(mContext))
                        .headers("token", TMSharedPUtil.getTMToken(mContext))
                        .params("id",item?.id!!)
                        .params("type",1)
                        .execute(object : StringCallback(){
                            override fun onSuccess(response: Response<String>?) {
                                val body = response?.body()
                                val baseBean = Gson().fromJson(body, BaseBean::class.java)
                                if (baseBean.code==1){
                                    datas[helper?.adapterPosition].laud_num++
                                    datas[helper?.adapterPosition].is_laud=1
                                    notifyDataSetChanged()
                                }else{
                                    GoLoginDialog(mContext).show()
                                }
                            }
                        })
            }
        }


    }
}